home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / Mesa-1.2.1 / demos / glxpixmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-05  |  3.3 KB  |  142 lines

  1. /* glxpixmap.c */
  2.  
  3.  
  4. /*
  5.  * A demonstration of using the GLXPixmap functions.
  6.  */
  7.  
  8.  
  9. #include <GL/gl.h>
  10. #include <GL/glx.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14.  
  15.  
  16. static GLXContext ctx;
  17. static XVisualInfo *visinfo;
  18. static GC gc;
  19.  
  20.  
  21.  
  22. static Window make_rgb_window( Display *dpy,
  23.                   unsigned int width, unsigned int height )
  24. {
  25.    int attrib[] = { GLX_RGBA,
  26.             GLX_RED_SIZE, 1,
  27.             GLX_GREEN_SIZE, 1,
  28.             GLX_BLUE_SIZE, 1,
  29.             None };
  30.    int scrnum;
  31.    XSetWindowAttributes attr;
  32.    unsigned long mask;
  33.    Window root;
  34.    Window win;
  35.  
  36.    scrnum = DefaultScreen( dpy );
  37.    root = RootWindow( dpy, scrnum );
  38.  
  39.    visinfo = glXChooseVisual( dpy, scrnum, attrib );
  40.    if (!visinfo) {
  41.       printf("Error: couldn't get an RGB, Double-buffered visual\n");
  42.       exit(1);
  43.    }
  44.  
  45.    /* window attributes */
  46.    attr.background_pixel = 0;
  47.    attr.border_pixel = 0;
  48.    attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
  49.    attr.event_mask = StructureNotifyMask | ExposureMask;
  50.    mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  51.  
  52.    win = XCreateWindow( dpy, root, 0, 0, width, height,
  53.                 0, visinfo->depth, InputOutput,
  54.                 visinfo->visual, mask, &attr );
  55.  
  56.    /* make an X GC so we can do XCopyArea later */
  57.    gc = XCreateGC( dpy, win, 0, NULL );
  58.  
  59.    ctx = glXCreateContext( dpy, visinfo, NULL, True );
  60.  
  61.    return win;
  62. }
  63.  
  64.  
  65. static GLXPixmap make_pixmap( Display *dpy, Window win,
  66.                    unsigned int width, unsigned int height )
  67. {
  68.    Pixmap pm;
  69.    GLXPixmap glxpm;
  70.  
  71.    pm = XCreatePixmap( dpy, win, width, height, visinfo->depth );
  72.  
  73.    glxpm = glXCreateGLXPixmap( dpy, visinfo, pm );
  74.  
  75.    return glxpm;
  76. }
  77.  
  78.  
  79.  
  80. static void event_loop( Display *dpy, GLXPixmap pm )
  81. {
  82.    XEvent event;
  83.  
  84.    while (1) {
  85.       XNextEvent( dpy, &event );
  86.  
  87.       switch (event.type) {
  88.      case Expose:
  89.         printf("Redraw\n");
  90.         /* copy the image from GLXPixmap to window */
  91.         XCopyArea( dpy, pm, event.xany.window,  /* src, dest */
  92.                gc, 0, 0, 300, 300,          /* gc, src pos, size */
  93.                0, 0 );                      /* dest pos */
  94.         break;
  95.      case ConfigureNotify:
  96.         /* nothing */
  97.         break;
  98.       }
  99.    }
  100. }
  101.  
  102.  
  103.  
  104. main( int argc, char *argv[] )
  105. {
  106.    Display *dpy;
  107.    Window win;
  108.    GLXPixmap pm;
  109.  
  110.    dpy = XOpenDisplay(NULL);
  111.  
  112.    win = make_rgb_window( dpy, 300, 300 );
  113.    pm = make_pixmap( dpy, win, 300, 300 );
  114.  
  115.    /* *VERY IMPORTANT*:  In Mesa's implementation of GLX, a GLXContext should
  116.     * first be bound to a window, just temporarily, before it can be bound to a
  117.     * GLXPixmap.  The reason is we sometimes (when a PsuedoColor visual is
  118.     * used in RGB mode) need to know the colormap associated with the context.
  119.     * The colormap can't be discovered from a pixmap but can be found with a
  120.     * window.
  121.     */
  122.  
  123.    glXMakeCurrent( dpy, win, ctx );  /*to make sure ctx is properly initialized*/
  124.    glXMakeCurrent( dpy, pm, ctx );
  125.  
  126.    /* Render an image into the pixmap */
  127.    glShadeModel( GL_FLAT );
  128.    glClearColor( 0.5, 0.5, 0.5, 1.0 );
  129.    glClear( GL_COLOR_BUFFER_BIT );
  130.    glViewport( 0, 0, 300, 300 );
  131.    glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
  132.    glColor3f( 0.0, 1.0, 1.0 );
  133.    glRectf( -0.75, -0.75, 0.75, 0.75 );
  134.    glFlush();
  135.  
  136.    /* when a redraw is needed we'll just copy the pixmap image to the window */
  137.  
  138.    XMapWindow( dpy, win );
  139.  
  140.    event_loop( dpy, pm );
  141. }
  142.